package com.example.teamtribe.main.club.clubintroduce;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.common.Back;
import com.example.teamtribe.database.Team;
import com.example.teamtribe.event.EventTeam;
import com.example.teamtribe.main.club.clubintroduce.update.UpdateIntroduceActivity;
import com.example.teamtribe.main.club.clubmembers.MembersModel;
import com.example.teamtribe.utils.Utility;
import com.example.zhouwei.library.CustomPopWindow;

import org.greenrobot.eventbus.EventBus;
import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

public class ClubIntroduceActivity extends BaseActivity implements View.OnClickListener{
    private Team team;
    private CustomPopWindow mCustomPopWindow;
    private ImageView portrait;
    private ImageView more;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clubintroduce);


        team=getIntent().getParcelableExtra("team");
        portrait = (ImageView)findViewById(R.id.iv_portrait);
        ((TextView)findViewById(R.id.tv_title)).setText(team.getName());
        more=(ImageView)findViewById(R.id.iv_more);
        hasAuthority();//获取是否有权限
        more.setOnClickListener(this);
        Glide.with(this).load(Utility.getUri(team.getPortrait())).into(portrait);
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager_introduce);
        viewPager.setAdapter(new ClubIntroducePagerAdapter(getSupportFragmentManager()));
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout_introduce);
        tabLayout.setupWithViewPager(viewPager);
        EventBus.getDefault().postSticky(new EventTeam(team));
    }
    private void handleLogic(View contentView){
        contentView.findViewById(R.id.tv_alter).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCustomPopWindow.dissmiss();
                Intent intent = new Intent(ClubIntroduceActivity.this, UpdateIntroduceActivity.class);
                intent.putExtra("team", team);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
//            case R.id.tv_alter:
//                Toast.makeText(ClubIntroduceActivity.this,"!!!",Toast.LENGTH_SHORT).show();
//                break;
            case R.id.iv_more:
                View contentView = LayoutInflater.from(ClubIntroduceActivity.this).inflate(R.layout.pop_introduce_alter,null);
                //处理popWindow 显示内容
                handleLogic(contentView);
                //创建并显示popWindow
                mCustomPopWindow= new CustomPopWindow.PopupWindowBuilder(ClubIntroduceActivity.this)
                        .setView(contentView)
                        .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                        .setBgDarkAlpha(0.7f) // 控制亮度
                        .create()
                        .showAsDropDown(more,0,0);
        }

    }
    @Override
    public void onResume() {
        super.onResume();
        List<Team> teams = DataSupport.where("realId = ?", team.getRealId() + "").find(Team.class);
        team=teams.get(0);
        ((TextView)findViewById(R.id.tv_title)).setText(team.getName());
        Glide.with(this).load(Utility.getUri(team.getPortrait())).into(portrait);
        EventBus.getDefault().postSticky(new EventTeam(team));
    }
    private void hasAuthority(){
        final int id=Utility.getUser().getRealId();
        new MembersModel().getPresident(team.getRealId(),new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                ArrayList<Integer> ids = bundle.getIntegerArrayList("ids");
                for (int i = 0; i < ids.size(); i++) {
                    if (id == ids.get(i)){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                more.setVisibility(View.VISIBLE);
                            }
                        });
                    }
                }
            }

            @Override
            public void failure(String meg) {
                showToast(meg);
            }
        });
        new MembersModel().getAdmins(id,new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                ArrayList<Integer> ids = bundle.getIntegerArrayList("ids");
                for (int i = 0; i < ids.size(); i++) {
                    if (id == ids.get(i)){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                more.setVisibility(View.VISIBLE);
                            }
                        });
                    }
                }
            }

            @Override
            public void failure(String meg) {
                showToast(meg);
            }
        });
    }
}